/**
 * 
 */
package com.iwords.service.object;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.Transient;

/**
 * @author qianxiang
 *
 */
@Entity
@Table(name="user_vacation")
public class UserVacation implements Serializable {
	@Id
	@JoinColumn(name ="userid")
	private UserEntity user;
	private Long vacationCount;
	@Temporal(javax.persistence.TemporalType.DATE)
	private Date expireDate;
	
	@Temporal(javax.persistence.TemporalType.TIMESTAMP)
	private Date createtime;
	@Temporal(javax.persistence.TemporalType.TIMESTAMP)
	private Date updatetime;
	
	@Transient
	private boolean valid;
	
	public Long getVacationCount() {
		return vacationCount;
	}
	public void setVacationCount(Long vacationCount) {
		this.vacationCount = vacationCount;
	}
	public Date getExpireDate() {
		return expireDate;
	}
	public void setExpireDate(Date expireDate) {
		this.expireDate = expireDate;
	}
	public Date getCreatetime() {
		return createtime;
	}
	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}
	public Date getUpdatetime() {
		return updatetime;
	}
	public void setUpdatetime(Date updatetime) {
		this.updatetime = updatetime;
	}
	public UserEntity getUser() {
		return user;
	}
	public void setUser(UserEntity user) {
		this.user = user;
	}
	public boolean isValid() {
		//System.out.println(expireDate);
		return expireDate==null?false:(expireDate.after(getFormatDateTime(getFormatDateTime(new Date(),"yyyyMMdd"),"yyyyMMdd"))?true:false);
	}
	public static String getFormatDateTime(java.util.Date currDate, String format) {
		SimpleDateFormat dtFormatdB = null;
		try {
			dtFormatdB = new SimpleDateFormat(format);
			return dtFormatdB.format(currDate);
		} catch (Exception e) {
			dtFormatdB = new SimpleDateFormat("yyyyMMdd");
			return dtFormatdB.format(currDate);
		}
	}
	public static Date getFormatDateTime(String currDate, String format) {
		SimpleDateFormat dtFormatdB = null;
		try {
			dtFormatdB = new SimpleDateFormat(format);
			return dtFormatdB.parse(currDate);
		} catch (Exception e) {
			dtFormatdB = new SimpleDateFormat("yyyyMMdd");
			try {
				return dtFormatdB.parse(currDate);
			} catch (Exception ex){}
		}
		return null;
	}
	public void setValid(boolean valid) {
		this.valid = valid;
	}
	@Override
	public String toString() {
		return "UserVacation [user=" + user + ", vacationCount=" + vacationCount + ", expireDate=" + expireDate + ", createtime=" + createtime + ", updatetime=" + updatetime
				+ ", valid=" + isValid() + "]";
	}
	
}
